const puppeteer = require('puppeteer')
const R = require('ramda')
const {parseJsonp, keysToLower} = require('./utils.js')
const detail = require('./detail.js')

/*
    STARTDATE:起息日期
    LISTDATE:上市日期
*/
const fields = [
    'BONDCODE', 'SNAME', 'SWAPSCODE', 'SECURITYSHORTNAME', 'STARTDATE', 'LISTDATE'
]

const mypick = R.pipe(
    R.pick(fields),
    keysToLower
)

const checkListDate = ({listdate}) => {
    if (listdate === '-') {
        return false
    }
    let ldate = new Date(listdate)
    return Date.now() - 86400000 > ldate.getTime()
}

async function fetchList(page) {
    let pages = -1
    let dataset = []
    page.on('response', resp => {
        //console.log(resp.url())
        if (resp.url().startsWith('http://dcfm.eastmoney.com/EM_Muti')) {
            //返回的格式"jQuery1123048543535627263656_1621419393782({...})"
            resp.text().then(txt => {
                let obj = parseJsonp(txt)
                if (pages === -1) {
                    pages = obj.pages
                }
                dataset = dataset.concat(obj.data.map(mypick))
            })
        }
    })
    await page.goto('http://data.eastmoney.com/kzz/default.html')
    for (let i = 0; i < pages - 1; i++) {
        await page.waitForTimeout(1000)
        await page.$eval('.pagerbox a:last-child', btn => {
            btn.click()
        })
    }
    return R.filter(checkListDate, dataset)
}

module.exports = fetchList

/*
run().catch(err => {
    console.log(err)
    process.exit(1)
})
*/
